<?php

namespace Home\Model;

use Think\Model;

/**
 * Description of GameModel
 * 游戏分类模型
 * @author xtceetg
 */
class GameCateModel extends Model {

    protected $tablename = "game_cate";

    
    function findChild(&$arr, $id) {

        $childs = array();
        foreach ($arr as $k => $v) {
            if ($v['pid'] == $id) {
                $childs[] = $v;
            }
        }

        return $childs;
    }

    function build_tree($root_id) {
        $rows=  $this->select();
        $childs = $this->findChild($rows, $root_id);
        if (empty($childs)) {
            return null;
        }
        foreach ($childs as $k => $v) {
            $rescurTree = $this->build_tree($v['cid']);
            if (null != $rescurTree) {
                $childs[$k]['childs'] = $rescurTree;
            }
        }
        return $childs;
    }

}
